package com.itheima.service.impl;

import com.itheima.constant.RedisMessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.exception4project.NoSuchOrderSettingException;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MobileOrderSettingService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MobileOrderSettingService.class)
@Transactional
public class MobileOrderSettingServiceImpl implements MobileOrderSettingService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Integer addOrderSetMealSetting(Map map) throws Exception {
        //从对象中取出 telephoneNumber
        String telephone = (String) map.get("telephone");
        //System.out.println(telephone);
        String teleId = telephone + RedisMessageConstant.SENDTYPE_ORDER;
        String verCode = jedisPool.getResource().get(teleId);
        if (verCode == null || verCode.length() == 0){
            throw new NoSuchOrderSettingException("验证码不存在");
        }else{
            if (!verCode.equals(map.get("validateCode"))){
                throw new NoSuchOrderSettingException("验证码输入错误");
            }else{
                //调用dao层方法将预约信息存入数据库中
                //该用户的预约时间是否设置了可预约人数
                //查询用户预约日期是否存在
                String orderDate = (String) map.get("orderDate");
                //System.out.println(orderDate);
                long countOrderDate = orderSettingDao.searchOrderSettingByDate(orderDate);
                if (countOrderDate == 0){
                    throw new NoSuchOrderSettingException("当前日期不可预约");
                }

                //已预约量是否已经大于或等于可预约人数
                //String  setmealId = (String) map.get("setmealId");
                Map<String,Integer> result = orderSettingDao.searchReservations(orderDate);
                if (result.get("number") <= result.get("reservations")){
                    throw new NoSuchOrderSettingException("当前日期预约人数已满");
                }

                //判断用户是否存在重复预约-->在此处还需判断，该用户是否存在 存在/不存在-->查询用户-->自动注册会员
                //member_id orderDate setmeal_id
                Member member = memberDao.findMemberByTelephone(telephone);
                if (member != null) {
                    Integer memberId = member.getId();
                    Date date = DateUtils.parseString2Date(orderDate);
                    String setmealId = (String) map.get("setmealId");
                    List<Order> orders = orderDao.searchOrderByMemberId(memberId, date, Integer.parseInt(setmealId));
                    if (orders != null && orders.size() != 0){
                        throw new NoSuchOrderSettingException("您已预约当前套餐，不可重复预约");
                    }
                }else{
                    //不存在自动注册 ==> 先将用户信息存入数据库中
                    //t_member t_order t_setmeal t_ordersetting
                    member = new Member();
                    member.setName((String) map.get("name"));
                    member.setPhoneNumber(telephone);
                    member.setRegTime(new Date());
                    member.setIdCard((String) map.get("idCard"));
                    member.setSex((String) map.get("sex"));
                    //插入当前预约用户信息到会员表中
                    memberDao.addMember(member);
                }
                //未预约存入预约信息 更新预约量信息
                String setmealId = (String) map.get("setmealId");
                Order order = new Order(member.getId(),DateUtils.parseString2Date(orderDate),Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,Integer.parseInt(setmealId));
                orderDao.addOrder(order);
                //预约成功 orderSetting 表reservations +1
                orderSettingDao.editOrderSettingReservations(DateUtils.parseString2Date(orderDate));

                return order.getId();

            }
        }
    }

    @Override
    public Map findById(Integer id) {
        Map order = orderDao.searchOrderById(id);
        return order;
    }
}